import QtQuick
import QtQuick3D
import QtQuick3D.Helpers
import "RusticManor"
import "MarsCuriosityRover"

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")
    View3D {
        id: view_3d
        anchors.fill: parent
        renderMode: View3D.Offscreen
        environment: SceneEnvironment {
            // depthTestEnabled: false
            // depthPrePassEnabled: false
            antialiasingMode: SceneEnvironment.MSAA
            antialiasingQuality: SceneEnvironment.High
            backgroundMode: SceneEnvironment.Color
            clearColor: "#808080"
        }

        //坐标网格显示
        AxisHelper {
            enableAxisLines: false
            enableXYGrid: true
            enableXZGrid: true
            enableYZGrid: true
            gridColor: "#000000"
            gridOpacity: 0.3
        }

        //光源
        DirectionalLight {
            position: Qt.vector3d(0, 110, 110)
            ambientColor: "#999999"

            brightness: 1.0
            eulerRotation: Qt.vector3d(0, 0, 0)
        }

        Node {
            // 放在3D场景原点处
            position: Qt.vector3d(0, 0, 0)
            // 组件旋转的支点
            // pivot: Qt.vector3d(50, 0, 0)
            //旋转
            // eulerRotation: Qt.vector3d(0, 0, 0)
            // 缩放
            // scale: Qt.vector3d(1, 1, 1)
            //我的世界
            RusticManor {
                id: rustic_manor
                scale: Qt.vector3d(100,100,100)
            }
            Node {
                position: Qt.vector3d(rustic_manor.x,rustic_manor.y+50,rustic_manor.z)
                Text {
                    antialiasing: true
                    anchors.centerIn: parent
                    text: "RusticManor"
                    font.bold: true
                    font.pointSize: 5
                }
            }

            //MarsCuriosityRover好奇号火星车
            Node {
                // position: Qt.vector3d(rustic_manor.x+100,rustic_manor.y,rustic_manor.z)
                pivot: Qt.vector3d(100,0,0)
                PropertyAnimation on eulerRotation.y {
                    from: 0
                    to: 360
                    duration: 20000
                    loops: Animation.Infinite
                    // running: cameraNode.isRotating
                }

                Node {
                    position: Qt.vector3d(0,50,0)
                    Text {
                        antialiasing: true
                        anchors.centerIn: parent
                        text: "MarsCuriosityRover"
                        font.bold: true
                        font.pointSize: 5
                    }
                }

                MarsCuriosityRover {
                    id: mars_curiosity_rover
                    // position: Qt.vector3d(rustic_manor.x+100,rustic_manor.y,rustic_manor.z)
                    scale: Qt.vector3d(100,100,100)
                }
            }

        }

        Node {
            id: node_camera
            //透视相机
            PerspectiveCamera {
                id: camera
                position: Qt.vector3d(200, 200, 200)
                //旋转角度
                eulerRotation: Qt.vector3d(315, 45, 0)
                //可视距离
                //clipnear不能设置为0
                clipNear: 1
                clipFar: 3000

                //可视角度
                fieldOfView: 90
                fieldOfViewOrientation: PerspectiveCamera.Horizontal
            }
        }
        WasdController {
            controlledObject: node_camera
        }
/*
        MouseArea {
            anchors.fill: parent
            acceptedButtons: Qt.LeftButton|Qt.RightButton|Qt.MiddleButton

            property int middleCx: 0
            property int middleCy: 0
            property int rightCx: 0
            property int rightCy: 0
            property bool middlePress: false
            property bool rightPress: false
            onEntered: {
                cursorShape = Qt.OpenHandCursor
            }
            onExited: {
                cursorShape = Qt.ArrowCursor
            }
            onClicked: {
                // if (mouse.button === Qt.LeftButton) {
                //     // 获取(x, y)鼠标点击处的PickResult
                //     var result = view3D.pick(mouse.x, mouse.y)
                //     if (result.objectHit) {
                //         // 获取该鼠标位置处的Model
                //         var pickedModel = result.objectHit
                //         if (redModel === pickedModel) {
                //             printText.text = "点击红色模型"
                //         } else if (blueModel === pickedModel) {
                //             printText.text = "点击蓝色模型"
                //         }
                //         console.log(pickedModel)
                //     } else {
                //         printText.text = "点击模型试试"
                //     }
                // }
            }
            onPressed: (mouse)=> {
                if (mouse.button === Qt.MiddleButton) {
                    cursorShape = Qt.CrossCursor
                    middleCx = mouse.x
                    middleCy = mouse.y
                    middlePress = true
                } else if (mouse.button === Qt.RightButton) {
                    cursorShape = Qt.ClosedHandCursor
                    rightCx = mouse.x
                    rightCy = mouse.y
                    rightPress = true
                } else if (mouse.button === Qt.LeftButton) {
                    cursorShape = Qt.PointingHandCursor
                }
            }
            onPositionChanged: (mouse)=> {
                if (middlePress) {
                    let intervalX = mouse.x-middleCx
                    let intervalY = mouse.y-middleCy
                    node_camera.eulerRotation.x = node_camera.eulerRotation.x-intervalY
                    node_camera.eulerRotation.y = node_camera.eulerRotation.y-intervalX
                    middleCx = mouse.x
                    middleCy = mouse.y
                }
                if (rightPress) {
                    let intervalX = mouse.x - rightCx
                    let intervalY = mouse.y - rightCy
                    camera.x = camera.x - (0.000027 * intervalX * camera.z * camera.fieldOfView * (640 / view_3d.width))
                    camera.y = camera.y + (0.000027 * intervalY * camera.z * camera.fieldOfView * (480 / view_3d.height))
                    rightCx = mouse.x
                    rightCy = mouse.y
                }
            }
            onReleased: (mouse)=> {
                cursorShape = Qt.OpenHandCursor
                if (mouse.button === Qt.MiddleButton) {
                    middlePress = false
                } else if (mouse.button === Qt.RightButton) {
                    rightPress = false
                }
            }

            onWheel: (wheel)=> {
                if (wheel.angleDelta.y < 0) {
                    camera.z = camera.z * 1.1
                } else {
                    camera.z = camera.z * 0.9
                }
            }
        }
*/
    }

}
